using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class FriendListTable : MonoBehaviour
{
	public float xPadding = 30f;

	public float yPadding = 10f;

	public int paddingRows = 2;

	public UIPanel scrollView;

	public UIWidget rowPrefab;

	public int buttonsInRow = 2;

	private FriendsDisplay activeDisplay;

	private int activeGroupId = -1;

	private List<UIWidget> rows = new List<UIWidget>();

	private InvitableFriends myFriends;

	private Vector3 scrollViewLastPosition = Vector3.zero;

	private int topRowIndex = -1;

	private float rowScale
	{
		get
		{
			return (scrollView.width - xPadding) / (float)rowPrefab.width;
		}
	}

	private float rowHeight
	{
		get
		{
			return rowScale * (float)rowPrefab.height;
		}
	}

	private float totalRowsInView
	{
		get
		{
			return Mathf.CeilToInt(scrollView.height / (rowHeight + yPadding)) + paddingRows;
		}
	}

	private void Awake()
	{
	}

	public void ShowFriendsTable(InvitableFriends friends, int groupId, FriendsDisplay display)
	{
		myFriends = friends;
		activeDisplay = display;
		activeGroupId = groupId;
		Debug.Log("ShowFriendsTable: " + myFriends.data.Count);
		scrollView.transform.localPosition = Vector3.zero;
		topRowIndex = 0;
		int num = (int)Mathf.Min(totalRowsInView * (float)buttonsInRow, friends.data.Count);
		for (int i = 0; i < num; i++)
		{
			int num2 = Mathf.FloorToInt(i / buttonsInRow);
			if (num2 >= rows.Count)
			{
				UIWidget component = NGUITools.AddChild(scrollView.gameObject, rowPrefab.gameObject).GetComponent<UIWidget>();
				component.name = GGFormat.IndexName(num2);
				rows.Add(component);
			}
			rows[num2].height = (int)rowHeight;
			rows[num2].width = (int)(scrollView.width - xPadding);
			float y = scrollView.height / 2f - (float)(num2 - 1) * (rowHeight + yPadding);
			Vector3 zero = Vector3.zero;
			zero.y = y;
			rows[num2].transform.localPosition = zero;
			int index = i % buttonsInRow;
			FriendsButton component2 = rows[num2].transform.GetChild(index).GetComponent<FriendsButton>();
			component2.Init(myFriends.data[i], groupId, display);
			component2.gameObject.SetActive(true);
			rows[num2].gameObject.SetActive(false);
			rows[num2].gameObject.SetActive(true);
		}
		for (int j = 1; j <= num % buttonsInRow; j++)
		{
			int index2 = buttonsInRow - j;
			Transform child = rows[rows.Count - 1].transform.GetChild(index2);
			child.gameObject.SetActive(false);
		}
		Vector3 localPosition = scrollView.transform.localPosition;
		localPosition.y -= rows[0].transform.localPosition.y - scrollView.height / 2f + rowHeight / 2f;
		scrollView.transform.localPosition = localPosition;
		scrollViewLastPosition = scrollView.transform.localPosition;
	}

	private UIWidget CreateTableRow(Vector3 position, UIWidget row)
	{
		return null;
	}

	private void Update()
	{
		if (myFriends == null || myFriends.data.Count <= 0)
		{
			return;
		}
		Vector3 vector = scrollView.transform.localPosition - scrollViewLastPosition;
		if (vector.y == 0f)
		{
			return;
		}
		float num = scrollView.height / 2f - scrollView.transform.localPosition.y;
		float num2 = (0f - scrollView.height) / 2f - scrollView.transform.localPosition.y;
		float num3 = -1f * Mathf.Sign(vector.y);
		int num4 = ((!(num3 > 0f)) ? (rows.Count - 1) : 0);
		int index = ((num3 > 0f) ? (rows.Count - 1) : 0);
		float num5 = rows[num4].transform.localPosition.y + num3 * rowHeight / 2f;
		float num6 = ((!(num3 > 0f)) ? num2 : num);
		for (bool flag = ((!(num3 > 0f)) ? (num5 > num6) : (num5 < num6)); flag && (!(num3 > 0f) || (topRowIndex - 1) * buttonsInRow >= 0) && (!(num3 < 0f) || (topRowIndex + num4 + 1) * buttonsInRow < myFriends.data.Count); flag = ((!(num3 > 0f)) ? (num5 > num6) : (num5 < num6)))
		{
			scrollViewLastPosition = scrollView.transform.localPosition;
			UIWidget uIWidget = rows[num4];
			UIWidget uIWidget2 = rows[index];
			Vector3 localPosition = uIWidget.transform.localPosition;
			localPosition.y += num3 * (rowHeight + yPadding);
			uIWidget2.transform.localPosition = localPosition;
			rows.RemoveAt(index);
			rows.Insert(num4, uIWidget2);
			topRowIndex = ((!(num3 > 0f)) ? (topRowIndex + 1) : (topRowIndex - 1));
			for (int i = 0; i < buttonsInRow; i++)
			{
				FriendsButton component = uIWidget2.transform.GetChild(i).GetComponent<FriendsButton>();
				int num7 = (topRowIndex + num4) * buttonsInRow + i;
				Debug.Log("friend index: " + num7);
				if (num7 >= myFriends.data.Count)
				{
					component.gameObject.SetActive(false);
					continue;
				}
				InvitableFriends.InvitableFriend friend = myFriends.data[num7];
				component.Init(friend, activeGroupId, activeDisplay);
				component.gameObject.SetActive(true);
			}
			num5 = rows[num4].transform.localPosition.y + num3 * rowHeight / 2f;
		}
	}
}
